﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.IO;
using System.Xml;
using System.Xml.XPath;
using AppConfigEditor;

namespace ConfigForm
{
    public partial class ConfigForm : UTLBASEFORM.MDICHILD
    {
        private FolderBrowserDialog fbd;
        private UTLDB.UTLXML xml;
        private string ratecode = "";
        private const string MsgNotConnect = "Kiểm tra kết nối không thành công!";
        private const string MsgConnected = "Kiểm tra kết nối thành công!";

        XmlDocument appDoc = new XmlDocument();
        private string configFileName = "App.config";
        readonly string localPath = Application.StartupPath;
        private string nodeToEdit = string.Empty;
        
        private string appConfigFileName = string.Empty;
        private string appDir = string.Empty;
        bool useXPath = false;

        BLL.TPriceUnit tpriceUnit = new BLL.TPriceUnit();

        public ConfigForm()
        {
            InitializeComponent();
        }
        public ConfigForm(string dir)
        {
            appDir = dir;
            InitializeComponent();
        }

        private void ConfigFom_Load(object sender, EventArgs e)
        {

            SetupMenu();
            LoadConfig(localPath +"\\"+ configFileName);
            LoadRate(ConfigurationManager.AppSettings["RatePathFile"]);
            LoadUnit();
            
            //            NumberTextBox = txtMailPort;
            NumberTextBox = txtPort;
            NumerTextBoxWithSeparator = txtMailPort;

            fbd = new FolderBrowserDialog();
            fbd.ShowNewFolderButton = false;
            fbd.Description = "Chọn thư mục mặc định";
            //
            gvPriceUnit.AutoGenerateColumns = false;
            gvPriceSeed.AutoGenerateColumns = false;
           LoadPriceUnit();
           LoadPriceSeed();
        }

        private void LoadUnit()
        {
            grdData.DataSource = GetData("sp_Select_tblUnit", null, null);
        }
        /// <summary>
        /// get data 
        /// </summary>
        /// <param name="strStoreName"></param>
        /// <param name="Para"></param>
        /// <returns></returns>
        private DataTable GetData(string strStoreName, string[] ParaName, string[] ParaValue)
        {
            UTLDB.UTLDB provider = new UTLDB.UTLDB();
            DataTable rs = new DataTable();
            try
            {
                if (ParaName != null && ParaValue != null)
                {
                    rs = provider.GetDataByStoredProcedure(strStoreName, ParaName, ParaValue);
                }
                else
                {
                    rs = provider.GetDataByStoredProcedure(strStoreName);
                }
            }
            catch (Exception ex)
            {
                //write log here
                MessageBox.Show(ex.ToString());
                rs = null;
            }
            return rs;
        }
        
        private void LoadRate(string strPathxml)
        {
            if (strPathxml.Equals(""))
            {
                return;
            }
            if (!File.Exists(strPathxml))
            {
                return;
            }
            xml = new UTLDB.UTLXML(strPathxml);
            DataTable dtRateResult = new DataTable();
            dtRateResult = xml.ReadAllInfor();
            for (int i = 0; i < dtRateResult.Columns.Count; i++)
            {
                int stt = i + 1;
                ListViewItem item = new ListViewItem(stt.ToString());
                item.SubItems.Add(dtRateResult.Columns[i].Caption);
                item.SubItems.Add(dtRateResult.Rows[0][i].ToString());
                listPrice.Items.Add(item);
            }
        }

        private void LoadConfig()
        {
            txtLocalConnection.Text = ReadConfig("LocalConnection");
            txtEmail.Text = ReadConfig("Email");
            txtDislayName.Text = ReadConfig("DisplayName");
            txtMailServerName.Text = ReadConfig("MailServerName");
            txtMailPort.Text = ReadConfig("MailPort");
            txtMailPass.Text = ReadConfig("MailPass");
            //txtUserName.Text = ReadConfig("UserName");
            txtServer.Text = ReadConfig("Server");
            txtPort.Text = ReadConfig("Port");
            txtDataBase.Text = ReadConfig("DataBase");
            txtUserID.Text = ReadConfig("UserID");
            txtPassword.Text = ReadConfig("Password");
            if (ReadConfig("Trusted_Connection").Equals("true"))
            {
                chbTrusted_Connection.Checked = true;
            }
            else
            {
                chbTrusted_Connection.Checked = false;
            }
            txtPathLogDefault.Text = ReadConfig("PathLogDefault");
            txtPathExPortDefault.Text = ReadConfig("PathExPortDefault");
            txtPathReportDefault.Text = ReadConfig("PathReportDefault");
            txtPathBackUpDataDefault.Text = ReadConfig("PathBackUpDataDefault");
            txtUnitVN.Text = ReadConfig("UnitVN");
            txtUnitUSD.Text = ReadConfig("UnitUSD");
            if (ReadConfig("OptionConnection").ToString().Equals("1"))
            {
                chbOptionConnection.Checked = false;
            }
            else
            {
                chbOptionConnection.Checked = true;
            }
        }

        private void LoadConfig(string path)
        {
            UTLDB.UTLXML _xml = new UTLDB.UTLXML(path);
            txtLocalConnection.Text = _xml.ReadTagInfor("LocalConnection");
            txtEmail.Text = _xml.ReadTagInfor("Email");
            txtDislayName.Text = _xml.ReadTagInfor("DisplayName");
            txtMailServerName.Text = _xml.ReadTagInfor("MailServerName");
            txtMailPort.Text = _xml.ReadTagInfor("MailPort");
            txtMailPass.Text = _xml.ReadTagInfor("MailPass");
            //txtUserName.Text = ReadConfig("UserName");
            txtServer.Text = _xml.ReadTagInfor("Server");
            txtPort.Text = _xml.ReadTagInfor("Port");
            txtDataBase.Text = _xml.ReadTagInfor("DataBase");
            txtUserID.Text = _xml.ReadTagInfor("UserID");
            txtPassword.Text = _xml.ReadTagInfor("Password");
            if (_xml.ReadTagInfor("Trusted_Connection").Equals("true"))
            {
                chbTrusted_Connection.Checked = true;
            }
            else
            {
                chbTrusted_Connection.Checked = false;
            }
            txtPathLogDefault.Text = _xml.ReadTagInfor("PathLogDefault");
            txtPathExPortDefault.Text = _xml.ReadTagInfor("PathExPortDefault");
            txtPathReportDefault.Text = _xml.ReadTagInfor("PathReportDefault");
            txtPathBackUpDataDefault.Text = _xml.ReadTagInfor("PathBackUpDataDefault");
            txtUnitVN.Text = _xml.ReadTagInfor("UnitVN");
            txtUnitUSD.Text = _xml.ReadTagInfor("UnitUSD");
            if (_xml.ReadTagInfor("OptionConnection").ToString().Equals("1"))
            {
                chbOptionConnection.Checked = false;
            }
            else
            {
                chbOptionConnection.Checked = true;
            }
        }

        private string ReadConfig(string strKeys)
        {
            return ConfigurationManager.AppSettings[strKeys];
        }

        private void UpdateConfig()
        {
            WriteConfig("LocalConnection", txtLocalConnection.Text);
            WriteConfig("Email", txtEmail.Text);
            WriteConfig("DisplayName", txtDislayName.Text);
            WriteConfig("MailServerName", txtMailServerName.Text);
            WriteConfig("MailPort", txtMailPort.Text);
            WriteConfig("MailPass", txtMailPass.Text);
            WriteConfig("Server", txtServer.Text);
            WriteConfig("Port", txtPort.Text);
            WriteConfig("DataBase", txtDataBase.Text);
            WriteConfig("UserID", txtUserID.Text);
            WriteConfig("Password", txtPassword.Text);
            if (chbTrusted_Connection.Checked)
            {
                WriteConfig("Trusted_Connection", "true");
            }
            else
            {
                WriteConfig("Trusted_Connection", "false");
            }
            WriteConfig("PathLogDefault", txtPathLogDefault.Text);
            WriteConfig("PathExPortDefault", txtPathExPortDefault.Text);
            WriteConfig("PathReportDefault", txtPathReportDefault.Text);
            WriteConfig("PathBackUpDataDefault", txtPathBackUpDataDefault.Text);
            WriteConfig("UnitVN", txtUnitVN.Text);
            WriteConfig("UnitUSD", txtUnitUSD.Text);
        }

        private void UpdateConfigXPath()
        {
            UpdateXPath("LocalConnection", txtLocalConnection.Text);
            UpdateXPath("Email", txtEmail.Text);
            UpdateXPath("DisplayName", txtDislayName.Text);
            UpdateXPath("MailServerName", txtMailServerName.Text);
            UpdateXPath("MailPort", txtMailPort.Text);
            UpdateXPath("MailPass", txtMailPass.Text);
            UpdateXPath("Server", txtServer.Text);
            UpdateXPath("Port", txtPort.Text);
            UpdateXPath("DataBase", txtDataBase.Text);
            UpdateXPath("UserID", txtUserID.Text);
            UpdateXPath("Password", txtPassword.Text);
            if (chbTrusted_Connection.Checked)
            {
                UpdateXPath("Trusted_Connection", "true");
            }
            else
            {
                UpdateXPath("Trusted_Connection", "false");
            }
            UpdateXPath("PathLogDefault", txtPathLogDefault.Text);
            UpdateXPath("PathExPortDefault", txtPathExPortDefault.Text);
            UpdateXPath("PathReportDefault", txtPathReportDefault.Text);
            UpdateXPath("PathBackUpDataDefault", txtPathBackUpDataDefault.Text);
            UpdateXPath("UnitVN", txtUnitVN.Text);
            UpdateXPath("UnitUSD", txtUnitUSD.Text);
        }

        private void WriteConfig()
        {
            //ConfigurationManager.AppSettings[strKeys] = strValue;
            UTLDB.UTLXML _xml = new UTLDB.UTLXML(localPath + "\\" + configFileName);
            _xml.UpdateTag("LocalConnection", txtLocalConnection.Text);
            _xml.UpdateTag("Email", txtEmail.Text);
            _xml.UpdateTag("DisplayName", txtDislayName.Text);
            _xml.UpdateTag("MailServerName", txtMailServerName.Text);
            _xml.UpdateTag("MailPort", txtMailPort.Text);
            _xml.UpdateTag("MailPass", txtMailPass.Text);
            _xml.UpdateTag("Server", txtServer.Text);
            _xml.UpdateTag("Port", txtPort.Text);
            _xml.UpdateTag("DataBase", txtDataBase.Text);
            _xml.UpdateTag("UserID", txtUserID.Text);
            _xml.UpdateTag("Password", txtPassword.Text);
            if (chbTrusted_Connection.Checked)
            {
                _xml.UpdateTag("Trusted_Connection", "true");
            }
            else
            {
                _xml.UpdateTag("Trusted_Connection", "false");
            }
            _xml.UpdateTag("PathLogDefault", txtPathLogDefault.Text);
            _xml.UpdateTag("PathExPortDefault", txtPathExPortDefault.Text);
            _xml.UpdateTag("PathReportDefault", txtPathReportDefault.Text);
            _xml.UpdateTag("PathBackUpDataDefault", txtPathBackUpDataDefault.Text);
            _xml.UpdateTag("UnitVN", txtUnitVN.Text);
            _xml.UpdateTag("UnitUSD", txtUnitUSD.Text);
        }

        private void WriteConfig(string strKeys, string strValue)
        {
            //ConfigurationManager.AppSettings[strKeys] = strValue;
            UTLDB.UTLXML _xml = new UTLDB.UTLXML(localPath +"\\"+ configFileName);
            _xml.UpdateTag("LocalConnection", txtLocalConnection.Text);
            _xml.UpdateTag("Email", txtEmail.Text);
            _xml.UpdateTag("DisplayName", txtDislayName.Text);
            _xml.UpdateTag("MailServerName", txtMailServerName.Text);
            _xml.UpdateTag("MailPort", txtMailPort.Text);
            _xml.UpdateTag("MailPass", txtMailPass.Text);
            _xml.UpdateTag("Server", txtServer.Text);
            _xml.UpdateTag("Port", txtPort.Text);
            _xml.UpdateTag("DataBase", txtDataBase.Text);
            _xml.UpdateTag("UserID", txtUserID.Text);
            _xml.UpdateTag("Password", txtPassword.Text);
            if (chbTrusted_Connection.Checked)
            {
                _xml.UpdateTag("Trusted_Connection", "true");
            }
            else
            {
                _xml.UpdateTag("Trusted_Connection", "false");
            }
            _xml.UpdateTag("PathLogDefault", txtPathLogDefault.Text);
            _xml.UpdateTag("PathExPortDefault", txtPathExPortDefault.Text);
            _xml.UpdateTag("PathReportDefault", txtPathReportDefault.Text);
            _xml.UpdateTag("PathBackUpDataDefault", txtPathBackUpDataDefault.Text);
            _xml.UpdateTag("UnitVN", txtUnitVN.Text);
            _xml.UpdateTag("UnitUSD", txtUnitUSD.Text);            
        }

        private void SetupMenu()
        {
            ShowBTN1(TextLableUpdate, ImageNameUpdate);
            ShowBTN2(TextLabelReset, ImageNameRefresh);
        }

        public override void btn1_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn đã sữa lại cấu hình của hệ thống, Có thể ứng dụng không chạy, nếu bạn cấu hình không đúng! Bạn có muốn lưu lại thay đổi không?", MsgTitile, MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
            {
                WriteConfig();
            }
        }

        public override void btn2_Click(object sender, EventArgs e)
        {
            LoadConfig();
        }

        private void btntPathLogDefault_Click(object sender, EventArgs e)
        {
            if (fbd.ShowDialog() == DialogResult.OK)
            {
                txtPathLogDefault.Text = fbd.SelectedPath;
            }
        }

        private void btnPathExPortDefault_Click(object sender, EventArgs e)
        {
            if (fbd.ShowDialog() == DialogResult.OK)
            {
                txtPathLogDefault.Text = fbd.SelectedPath;
            }
        }

        private void btnPathReportDefault_Click(object sender, EventArgs e)
        {
            if (fbd.ShowDialog() == DialogResult.OK)
            {
                txtPathLogDefault.Text = fbd.SelectedPath;
            }
        }

        private void btnPathBackUpDataDefault_Click(object sender, EventArgs e)
        {
            if (fbd.ShowDialog() == DialogResult.OK)
            {
                txtPathLogDefault.Text = fbd.SelectedPath;
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {

        }

        private void listPrice_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                txtPrice.Text = listPrice.Items[listPrice.SelectedIndices[0]].SubItems[2].Text;
                ratecode = listPrice.Items[listPrice.SelectedIndices[0]].SubItems[1].Text;
            }
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            UTLDB.UTLDB provider = new UTLDB.UTLDB();
            if (!chbOptionConnection.Checked)
            {
                if (!provider.CheckConnect(txtLocalConnection.Text))
                {
                    MessageBox.Show(MsgNotConnect, MsgTitile);
                }
                else
                {
                    MessageBox.Show(MsgConnected, MsgTitile);
                    WriteConfig("LocalConnection", this.txtLocalConnection.Text);
                    Application.Restart();
                }
            }
            else
            {
                string _strConnectionString = "";
                _strConnectionString = "Server = ";
                _strConnectionString += txtServer.Text.Trim();
                _strConnectionString += ";Database = ";
                _strConnectionString += txtDataBase.Text.Trim();
                _strConnectionString += ";User ID = ";
                _strConnectionString += txtUserID.Text.Trim();
                _strConnectionString += ";Password = ";
                _strConnectionString += txtPassword.Text.Trim();
                _strConnectionString += ";Trusted_Connection = ";
                _strConnectionString += chbTrusted_Connection.Checked.ToString();
                _strConnectionString += ";";
                if (!provider.CheckConnect(_strConnectionString))
                {
                    MessageBox.Show(MsgNotConnect, MsgTitile);
                }
                else
                {
                    MessageBox.Show(MsgConnected, MsgTitile);
                    WriteConfig("Server", txtServer.Text.Trim());
                    WriteConfig("DataBase", txtDataBase.Text.Trim());
                    WriteConfig("Port", txtPort.Text.Trim());
                    WriteConfig("UserID", txtUserID.Text.Trim());
                    WriteConfig("Password", txtPassword.Text.Trim());
                    WriteConfig("Trusted_Connection", chbTrusted_Connection.Checked.ToString());
                    Application.Restart();
                }
            }


        }

        private void chbOptionConnection_CheckedChanged(object sender, EventArgs e)
        {
            if (chbOptionConnection.Checked)
            {
                WriteConfig("OptionConnection", "2");
            }
            else
            {
                WriteConfig("OptionConnection", "1");
            }
        }


        ///// <summary>
        ///// Update attribute value and save XML/Config document.
        ///// </summary>
        //private void UpdateXPath()
        //{
        //    UnicodeEncoding uniEncoding = new UnicodeEncoding();
        //    XmlNode addNode = appDoc.SelectSingleNode(cbNodes.SelectedItem + "/@" + nodeToEdit);
        //    XmlNode valueNode = addNode.SelectSingleNode(".");

        //    valueNode.Value = txtValue.Text;
        //    StringBuilder sb = new StringBuilder(appDoc.InnerXml.Length);
        //    sb.Append(appDoc.InnerXml);
        //    File.WriteAllLines(appConfigFileName, new string[] { appDoc.InnerXml }, Encoding.UTF8);
        //}

        private void UpdateXPath(string strKey, string strValue)
        {
            UnicodeEncoding uniEncoding = new UnicodeEncoding();
            XmlNode addNode = appDoc.SelectSingleNode("configuration/appSettings/add[@key='" + strKey + "']");
            XmlNode valueNode = addNode.SelectSingleNode("@value");
            valueNode.InnerText = strValue;
            StringBuilder sb = new StringBuilder(appDoc.InnerXml.Length);
            sb.Append(appDoc.InnerXml);
            File.WriteAllLines(appConfigFileName, new string[] { appDoc.InnerXml }, Encoding.UTF8);
        }

        private void LoadConfigFileData()
        {
            useXPath = false;
            XPathDocument doc = null;
            try
            {
                appConfigFileName = localPath + "\\" + configFileName;
                XmlDocument xdoc = new XmlDocument();
                doc = new XPathDocument(appConfigFileName);
            }
            catch (IOException ioe)
            {
                string msg = configFileName + " is not found in this directory:" + localPath + "\nSelect App.config file";
                MessageBox.Show(msg);
                doc = LoadCopyFile();
            }
            LoadDocument(doc);
            ReadNodes(appConfigFileName);
        }

        private void LoadDocument(XPathDocument doc)
        {
            try
            {
                appDoc.Load(appConfigFileName);
                XPathNavigator rootElement = doc.CreateNavigator().SelectSingleNode("/*");
                TreeNode root = new TreeNode(rootElement.LocalName);
                //tvNodes.Nodes.Add(root);
                XmlNodeList addNodeList = appDoc.SelectNodes("configuration/appSettings/add");
                foreach (XmlNode node in addNodeList)
                {
                    TreeNode eltNode = new TreeNode(node.SelectSingleNode("@key").InnerText);
                    root.Nodes.Add(eltNode);
                }
                //tvNodes.ExpandAll();
            }
            catch { }
        }

        private XPathDocument LoadCopyFile()
        {
            XPathDocument doc = null;
            try
            {
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.ShowDialog();
                appConfigFileName = ofd.FileName;
                if (string.IsNullOrEmpty(appConfigFileName))
                    return null;
                File.Copy(appConfigFileName, localPath + "\\" + configFileName, true);
                appConfigFileName = localPath + "\\" + configFileName;

                File.SetAttributes(appConfigFileName, FileAttributes.Normal);
                doc = new XPathDocument(appConfigFileName);
                System.Threading.Thread.Sleep(1000);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return doc;
        }

        private void ReadNodes(string appConfigFileName)
        {
            ConfigReader cr = new ConfigReader();
            string[] nodes = cr.GetNodes(appConfigFileName);
        }

        /// <summary>
        /// update data on row
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <param name="active"></param>
        private void UpdateRow(int id, string name, bool active)
        {
            UTLDB.UTLDB provider = new UTLDB.UTLDB();
            string[] paraName = { "@ID", "@Name", "@Active" };
            object[] paravalue ={ id, name, active };
            provider.ExecuteNonQuery("sp_Update_tblUnit", paraName, paravalue);
        }
        private void grdData_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            
        }

        private void grdData_DataSourceChanged(object sender, EventArgs e)
        {
            
        }

        private void grdData_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            
        }

        private void grdData_BindingContextChanged(object sender, EventArgs e)
        {
            
        }

        private void grdData_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            DataTable dtSource = (DataTable)((DataGridView)sender).DataSource;
            DataTable dtChanged = dtSource.GetChanges();
            if (dtChanged == null)
            {
                return;
            }
            int _sizeData = dtChanged.Rows.Count;
            if (_sizeData < 1)
            {
                return;
            }
            //DBNull dbnull = new DBNull();
            //System.Runtime.Serialization.IFormatterConverter iformatconvert;
            //iformatconvert.Convert(1,
            //System.Runtime.Serialization.SerializationInfo inf=new System.Runtime.Serialization.SerializationInfo(typeof(bool),
            //dbnull.GetObjectData(
            for (int i = 0; i < _sizeData; i++)
            {
                UpdateRow(int.Parse(dtChanged.Rows[i]["ID"].ToString()), dtChanged.Rows[i]["Name"].ToString(), bool.Parse(dtChanged.Rows[i]["Active"].ToString()));
            }
        }

        /// <summary>
        /// Change price
        /// </summary>

        private void LoadPriceUnit() {

            LoadCmbType();
            LoadCmbUnit();
            LoadCmbMeterial();
            gvPriceUnit.DataSource = tpriceUnit.GetPriceUnit();
            

          
        }
        private void LoadCmbType() {
            DataTable dt =tpriceUnit.GetType();
            
            cmdType.DataSource = dt;
            cmdType.DisplayMember = "Name";// dt.Columns["Name"];
            cmdType.ValueMember = "ID";// dt.Columns["ID"];

        }
        private void LoadCmbUnit()
        {
            DataTable dt = tpriceUnit.GetUnit();
            cmdUnit.DataSource = dt;
            cmdUnit.DisplayMember = "Name";// dt.Columns["Name"];
            cmdUnit.ValueMember = "ID";// dt.Columns["ID"];

        }
        private void LoadCmbMeterial()
        {
            DataTable dt = tpriceUnit.GetMaterial();
            cmdMaterial.DataSource = dt;
            cmdMaterial.DisplayMember = "Name";// dt.Columns["Name"];
            cmdMaterial.ValueMember = "ID";// dt.Columns["ID"];

        }
        
        //
        private void LoadPriceSeed()
        {
           
            LoadCmbSeedType();

            LoadCmbQuality();

            gvPriceSeed.DataSource = tpriceUnit.GetSeed();
       }
        private void LoadCmbSeedType()
        {
            DataTable dt = tpriceUnit.GetSeedType();

            cmbTypeSeed.DataSource = dt;
            cmbTypeSeed.DisplayMember = "Name";// dt.Columns["Name"];
            cmbTypeSeed.ValueMember = "Sym";// dt.Columns["ID"];

        }
        private void LoadCmbQuality()
        {
            DataTable dt = tpriceUnit.GetQuality();

            cmdQuality.DataSource = dt;
            cmdQuality.DisplayMember = "Name";// dt.Columns["Name"];
            cmdQuality.ValueMember = "Name";// dt.Columns["ID"];

        }

        private void gvPriceUnit_RowLeave(object sender, DataGridViewCellEventArgs e)
        {
            UpdateRow(e.RowIndex);
        }

        private void UpdateRow(int rowindex)
        {
            DataTable dtTemp = (DataTable)gvPriceUnit.DataSource;
            BLL.CPriceUnit price;
            for (int i = 0; i < dtTemp.Rows.Count; i++)
            {
                //neu dong la khong doi, thi bo qua
                if(IsUnchangeRow(dtTemp.Rows[i]))
                {
                    continue;
                }
                //neu dong la edit thi update
                if (IsModifiedRow(dtTemp.Rows[i]))
                {
                    price = new global::ConfigForm.BLL.CPriceUnit();

                  //  price.ID = dtTemp.Rows[rowindex]["ID"];
                   // price.IdType = dtTemp.Rows[rowindex]["IdType"];
                }
                //neu dong la delete thi xoa
                //neu dong la new thi insert
                

            }
            
        }
       
    }
}